#include "Socket.hpp"

static void usage(std::string str)
{
    std::cout<<str<<std::endl;
}
int main(int argc,const char* argv[])
{
    if(argc!=3)
    {
        usage(argv[0]);
        exit(3);
    }
    std::string serverip = argv[1];
    uint16_t serverport = atoi(argv[2]);
    bool alive = false;
    int sock = 0;
    std::string line;
    while(true)
    {
        if(!alive)
        {
            int sock=Sock::Socket();
            Sock::Connect(sock,serverip,serverport);
            alive=true;
        }
        while (true)
        {
            std::cout << "请输入：";
            std::getline(std::cin, line);
            if (line == "quit")
                break;
            ssize_t s = send(sock, line.c_str(), line.size(), 0);
            if (s > 0)
            {
                char buffer[1024];
                ssize_t ss = recv(sock, buffer, sizeof(buffer) - 1, 0);
                if (ss > 0)
                {
                    buffer[s] = 0;
                    std::cout << "server 回显# " << buffer << std::endl;
                }
                else if (ss = 0)
                {
                    alive = false;
                    close(sock);
                }
            }
            else
            {
                alive = false;
                close(sock);
            }
        }
    }
    return 0;
}